package com.atguigu.study.bootredis.ltp;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jiangta
 * @version 1.0
 * @date 2020/12/14 11:29 上午
 */
public class Ltp {
    // webapi接口地址
    public static final String WEBTTS_URL = "http://ltpapi.xfyun.cn/v1/ke";
    // 应用ID
    private static final String APPID = "5fd19a5d";
    // 接口密钥
    private static final String API_KEY = "daffc9d99eabae2ab17d4b5030b44aa1";
    // 文本
    private static final String TEXT = "打开页面物联网";


    private static final String TYPE = "dependent";

    public static void main(String[] args) throws IOException {
        System.out.println(TEXT.length());
        Map<String, String> header = buildHttpHeader();
        String result = HttpUtil.doPost1(WEBTTS_URL, header, "text=" + URLEncoder.encode(TEXT, "utf-8"));
        System.out.println("itp 接口调用结果：" + result);
    }

    /**
     * 组装http请求头
     */
    public static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
        String curTime = System.currentTimeMillis() / 1000L + "";
        String param = "{\"type\":\"" + TYPE +"\"}";
        String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
        String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        header.put("X-Param", paramBase64);
        header.put("X-CurTime", curTime);
        header.put("X-CheckSum", checkSum);
        header.put("X-Appid", APPID);
        return header;
    }
}
